<template>
  <el-dialog width="800px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="100px"
      label-position="right"
      class="submit-form"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="收款日期" prop="receiptDate">
            <el-date-picker
              v-model="submitForm.receiptDate"
              style="width: 100%"
              type="date"
              placeholder="请选择收款日期"
              value-format="yyyy-MM-dd"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户" prop="customerId">
            <customer-query
              ref="customerQuery"
              :disabled="submitForm.id!==''"
              @choose="handleChooseCustomer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="订单" prop="orderId">
            <sale-order-query
              ref="orderQuery"
              :disabled="submitForm.id!==''||submitForm.customerId===''"
              :query="{ paymentStateList: ['0','1'],orderStateList: ['0','1','2'], customerId: submitForm.customerId }"
              @choose="handleChooseOrder"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-descriptions v-if="orderInfo.id" style="padding-left: 100px" :column="columnNum">
            <el-descriptions-item label="客户">
              {{ orderInfo.customerName }}
            </el-descriptions-item>
            <el-descriptions-item label="品名">
              {{ productInfo.moldName }}
            </el-descriptions-item>
            <el-descriptions-item label="颜色">
              {{ productInfo.color }}
            </el-descriptions-item>
            <el-descriptions-item label="材质">
              {{ productInfo.material }}
            </el-descriptions-item>
            <el-descriptions-item label="尺码">
              {{ productInfo.size }}
            </el-descriptions-item>
            <el-descriptions-item label="长度">
              {{ productInfo.length }}
            </el-descriptions-item>
<!--            <el-descriptions-item label="总数">
              {{ orderInfo.quantity }}
            </el-descriptions-item>-->
            <el-descriptions-item label="总金额">
              <money-format v-model="orderInfo.amount" />
            </el-descriptions-item>
            <el-descriptions-item label="已付款" content-class-name="red">
              <money-format v-model="orderInfo.paidAmount" />
            </el-descriptions-item>
            <el-descriptions-item label="待付款" content-class-name="red">
              <money-format v-model="orderInfo.remainingAmount" />
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款方式" prop="receiptMethod">
            <dict-select v-model="submitForm.receiptMethod" dict-key="payment_method" placeholder="请选择收款方式" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款金额" prop="amount">
            <el-input v-model="submitForm.amount" type="number" placeholder="请输入收款金额" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="submitForm.remark" type="textarea" placeholder="请输入备注" :autosize="{minRows:2}" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateReceiptById, saveReceipt, selectReceiptById } from '@/api/financial/receipt'
import { order_detail } from '@/api/sale/order'
import moment from 'moment'
/**
 * @author Administrator
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      columnNum: 3,
      submitForm: {
        id: '',
        receiptDate: moment(new Date()).format('YYYY-MM-DD'),
        customerId: '',
        orderId: '',
        amount: '',
        receiptMethod: ''
      },
      orderInfo: {},
      productInfo: {},
      submitFormRules: {
        receiptDate: [{ required: true, message: '请选择收款日期', trigger: 'change' }],
        customerId: [{ required: true, message: '请选择客户', trigger: 'change' }],
        orderId: [{ required: true, message: '请选择订单', trigger: 'change' }],
        amount: [{ required: true, message: '请输入收款金额', trigger: 'change' }],
        receiptMethod: [{ required: true, message: '请选择收款方式', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增收款'
        return
      }
      this.dialogTitle = '修改收款'
      this.formLoading = true
      selectReceiptById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.$refs.customerQuery.init(resp.data.customerName)
          order_detail(this.submitForm.orderId).then(res => {
            if (resp.success) {
              this.orderInfo = res.data
              this.productInfo = res.data.productInfo
              this.$refs.orderQuery.init(res.data)
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveReceipt
          if (this.submitForm.id) {
            request = updateReceiptById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleChooseOrder(row) {
      if (row) {
        this.submitForm.orderId = row.id
        this.orderInfo = row
        this.productInfo = row.productInfo
      }
    },
    handleChooseCustomer(row) {
      if (row) {
        this.submitForm.customerId = row.id
      }
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
